package com.ruoyi.fantasy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.huanzhidadi.fantasy.bean.domain.FantasyChemistFormula;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fantasy.domain.FantasyChemistFormulaBO;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.mapper.FantasyItemtypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyChemistFormulaMapper;
import com.ruoyi.fantasy.service.IFantasyChemistFormulaService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 药剂配方Service业务层处理
 * 
 * @author wanghui
 * @date 2020-09-10
 */
@Service
public class FantasyChemistFormulaServiceImpl implements IFantasyChemistFormulaService {
    @Resource
    private FantasyChemistFormulaMapper fantasyChemistFormulaMapper;

    @Resource
    private FantasyItemtypeMapper fantasyItemtypeMapper;


    /**
     * 查询药剂配方
     * 
     * @param id 药剂配方ID
     * @return 药剂配方
     */
    @Override
    public FantasyChemistFormula selectFantasyChemistFormulaById(String id)
    {
        return fantasyChemistFormulaMapper.selectFantasyChemistFormulaById(id);
    }

    /**
     * 查询药剂配方列表
     * 
     * @param fantasyChemistFormula 药剂配方
     * @return 药剂配方
     */
    @Override
    public List<FantasyChemistFormulaBO> selectFantasyChemistFormulaList(FantasyChemistFormula fantasyChemistFormula)
    {
        return fantasyChemistFormulaMapper.selectFantasyChemistFormulaList(fantasyChemistFormula);
    }

    /**
     * 新增药剂配方
     * 
     * @param fantasyChemistFormula 药剂配方
     * @return 结果
     */
    @Override
    public int insertFantasyChemistFormula(FantasyChemistFormula fantasyChemistFormula)
    {
        fantasyChemistFormula.setCreateTime(DateUtils.getNowDate());
        return fantasyChemistFormulaMapper.insertFantasyChemistFormula(fantasyChemistFormula);
    }

    /**
     * 修改药剂配方
     * 
     * @param fantasyChemistFormula 药剂配方
     * @return 结果
     */
    @Override
    public int updateFantasyChemistFormula(FantasyChemistFormula fantasyChemistFormula)
    {
        fantasyChemistFormula.setUpdateTime(DateUtils.getNowDate());
        return fantasyChemistFormulaMapper.updateFantasyChemistFormula(fantasyChemistFormula);
    }

    /**
     * 删除药剂配方对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFantasyChemistFormulaByIds(String ids)
    {
        return fantasyChemistFormulaMapper.deleteFantasyChemistFormulaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除药剂配方信息
     * 
     * @param id 药剂配方ID
     * @return 结果
     */
    @Override
    public int deleteFantasyChemistFormulaById(String id)
    {
        return fantasyChemistFormulaMapper.deleteFantasyChemistFormulaById(id);
    }

    @Override
    public void transforData(List<FantasyChemistFormulaBO> list) {
        List<FantasyItemType> fantasyItemTypeList = fantasyItemtypeMapper.selectFantasyItemtypeList(new FantasyItemType());
        Map<String, FantasyItemType> fantasyItemTypeMap = fantasyItemTypeList.stream().collect(
                Collectors.toMap(FantasyItemType::getId, fantasyItemType->fantasyItemType));
        for (FantasyChemistFormulaBO fantasyChemistFormulaBO : list) {
            if (StringUtils.isNotEmpty(fantasyChemistFormulaBO.getFactor1())) {
                fantasyChemistFormulaBO.setFactor1Str(fantasyItemTypeMap.get(fantasyChemistFormulaBO.getFactor1()).getName());
            }
            if (StringUtils.isNotEmpty(fantasyChemistFormulaBO.getFactor2())) {
                fantasyChemistFormulaBO.setFactor2Str(fantasyItemTypeMap.get(fantasyChemistFormulaBO.getFactor2()).getName());
            }
            if (StringUtils.isNotEmpty(fantasyChemistFormulaBO.getResult1())) {
                fantasyChemistFormulaBO.setResult1Str(fantasyItemTypeMap.get(fantasyChemistFormulaBO.getResult1()).getName());
            }
            if (StringUtils.isNotEmpty(fantasyChemistFormulaBO.getResult2())) {
                fantasyChemistFormulaBO.setResult2Str(fantasyItemTypeMap.get(fantasyChemistFormulaBO.getResult2()).getName());
            }
            if (StringUtils.isNotEmpty(fantasyChemistFormulaBO.getResult3())) {
                fantasyChemistFormulaBO.setResult3Str(fantasyItemTypeMap.get(fantasyChemistFormulaBO.getResult3()).getName());
            }
        }
    }
}
